package logic

import (
	"context"
	"fmt"

	"go-zero-mall/app/admin/cmd/rpc/internal/svc"
	"go-zero-mall/app/admin/cmd/rpc/pd"

	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/logx"
)

type MemeberLevelListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewMemeberLevelListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MemeberLevelListLogic {
	return &MemeberLevelListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 会员等级
func (l *MemeberLevelListLogic) MemeberLevelList(in *pd.UmsMemberLevelReq) (*pd.UmsMemberLevelResp, error) {
	countBuilder := l.svcCtx.UmsMemberLevelModel.CountBuilder("*")
	rowBuilder := l.svcCtx.UmsMemberLevelModel.RowBuilder()
	fmt.Println(in.DefaultStatus)
	if in.DefaultStatus != 0 {
		countBuilder = countBuilder.Where("default_status=?", in.DefaultStatus)
		rowBuilder = rowBuilder.Where("default_status=?", in.DefaultStatus)
	}
	d, err := l.svcCtx.UmsMemberLevelModel.FindByPageParam(l.ctx, rowBuilder, in.PageNum, in.PageSize)
	if err != nil {
		return nil, err
	}
	var resp []*pd.UmsMemberLevel
	copier.Copy(&resp, d)
	total, err := l.svcCtx.UmsMemberLevelModel.FindCount(l.ctx, countBuilder)
	if err != nil {
		return nil, err
	}
	return &pd.UmsMemberLevelResp{L: resp, Total: total}, nil
}
